package com.jumi.microservice.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.dto.comment.admin.*;
import com.jumi.microservice.common.base.dto.ExportExcelCommonResponse;
import com.jumi.microservice.service.admin.AdminSpecialService;
import com.jumi.microservice.service.admin.SelfWaitSendListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Scw
 * @Date 2020-08-04 17:54
 */
@Api(tags = {"后台敏感词相关接口"})
@RestController
@RequestMapping("/adminSpecial")
public class AdminSpecialController extends BaseController {
    @Resource
    private AdminSpecialService adminSpecialService;

    @GetMapping("/specialAdminList")
    @ApiOperation("敏感词列表")
    TableDataInfo<SensitiveKeywordListResponse> commentAdminList(SensitiveKeywordListRequest sensitiveKeywordListRequest) {
        //获取当前用户Id
        //sensitiveKeywordListRequest.setUid(getUid());
        //sensitiveKeywordListRequest.setUid(10119L);
        return adminSpecialService.spaecialAdminList(sensitiveKeywordListRequest);
    }

    /**
     *   * 是否开启
     *   @param  : 0:开启 1否
     * @param sensitiveStatusRequest
     * @return
     */
    @PutMapping("/editSpecialStatus")
    @ApiOperation("是否开启")
    public ResponseResult<Boolean> editSpecialStatus(SensitiveStatusRequest sensitiveStatusRequest) {
        //sensitiveStatusRequest.setUid(getUid());
        //sensitiveStatusRequest.setUid(10119L);
        ResponseResult<Boolean> result = new ResponseResult<>();
        result.setData(adminSpecialService.editSpecialStatus(sensitiveStatusRequest));
        return result;
    }

    /**
     * 删除
     * @param sensitiveKeywordDelRequest
     * @return
     */
    @PutMapping("/isdelSpecial")
    @ApiOperation("是否删除关键词")
    public ResponseResult<Boolean> isdelSpecial(SensitiveKeywordDelRequest sensitiveKeywordDelRequest) {
        //sensitiveKeywordDelRequest.setUid(getUid());
        //sensitiveKeywordDelRequest.setUid(10119L);
        ResponseResult<Boolean> result = new ResponseResult<>();
        result.setData(adminSpecialService.isdelSpecial(sensitiveKeywordDelRequest));
        return result;
    }

    @PutMapping("/editSensitive")
    @ApiOperation("编辑敏感词")
    public ResponseResult<Boolean> editSensitive(SensitiveKeywordRequest sensitiveKeywordRequest) {
        sensitiveKeywordRequest.setUpdataUser(getUid());//token中获取 uid
        sensitiveKeywordRequest.setUpdataUsername(getUserName());//token中获取 username
        //sensitiveKeywordRequest.getAuthorization();//添加人token 需处理成用户名存入表中

        ResponseResult<Boolean> result = new ResponseResult<>();
        result.setData(adminSpecialService.editSensitive(sensitiveKeywordRequest));
        return result;
    }

    @PostMapping("/addSpecial")
    @ApiOperation("敏感词添加")
    public ResponseResult<Boolean> addSpecial(SensitiveKeywordRequest sensitiveKeywordRequest) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        //获取当前用户ID
        //commentApiAddRequest.setUid(getApiUserId()); 前台需要用getApiUserId方法
        sensitiveKeywordRequest.setCreatUser(getUid());//token中获取 uid
        sensitiveKeywordRequest.setCreatUsername(getUserName());//token中获取 username
        result.setData(adminSpecialService.addSpecial(sensitiveKeywordRequest));
        return result;
    }

    @ApiOperation("导出")
    @PostMapping("/exportExcel")
    public void exportExcel(SenitiveExportRequest request, HttpServletResponse httpServerResponse) throws IOException {
        ExportExcelCommonResponse<SensitiveExportResponse> exportExcelResponseExportExcelCommonResponse = adminSpecialService.exportExcel(request);
        httpServerResponse.setContentType("application/vnd.ms-excel");
        httpServerResponse.setCharacterEncoding("utf-8");
        httpServerResponse.setHeader("Content-disposition", "attachment;filename=" + exportExcelResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServerResponse.getOutputStream(), SensitiveExportResponse.class).sheet("sheet1").doWrite(exportExcelResponseExportExcelCommonResponse.getData());
    }

    /**
     * 导入
     */
    @ApiOperation("导入")
    @PostMapping("/excelImport")
    public boolean excelImport(HttpServletRequest request) throws IOException {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        /*SenitiveImplodRequest.setCreatUser(getUid());//token中获取 uid
        SenitiveImplodRequest.setCreatUsername(getUserName());//token中获取 username*/
        EasyExcel.read(request.getInputStream(), SenitiveImplodRequest.class, new SelfWaitSendListener(adminSpecialService)).sheet().doRead();
        return true;
    }



}
